using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop;
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Try_TryAppliction : Shop, IRequiresSessionState
{
	protected RadioButtonList RadioSex;
	protected TextBox ApplicationBox;
	protected Label lbAppliError;
	protected RadioButtonList AddressList;
	protected Button NewAddress;
	protected PlaceHolder SelectAddress;
	protected Button SelOldAddress;
	protected DropDownList DropProvince;
	protected DropDownList DropCity;
	protected TextBox ZipCodeBox;
	protected Label lbCodeError;
	protected TextBox AddressBox;
	protected Label lbAddressError;
	protected TextBox UserNameBox;
	protected Label lbNameError;
	protected TextBox TelBox;
	protected Label lbTelError;
	protected TextBox HomeTelBox;
	protected Label lbHomeTelError;
	protected HtmlTable tabAddress;
	protected Label lbTotalError;
	protected TextBox TextBox1;
	protected CheckBox AgreeMent;
	protected ImageButton Submit;
	protected HtmlForm form1;
	public string UserName = "";
	protected int ProductID = 0;
	protected bool IsNewAddress = true;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Init(object sender, EventArgs e)
	{
		try
		{
			Template.BindTemplateToPage(this.Page, "SaleProduct");
		}
		catch (Exception ex)
		{
			base.WriteTemErrMsg(ex.Message.ToString());
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.user.CheckUserLogin())
		{
			this.WriteErrMsg("", Language.GetMemberMsg("Common/NotLogin", new string[0]), this.InstallDir + "Passport/Login.aspx?ComeUrl=" + base.Server.UrlEncode(Fetch.CurrentUrl));
		}
		if (base.Request.QueryString["ProductID"] != null)
		{
			try
			{
				this.ProductID = int.Parse(base.Request.QueryString["ProductID"]);
			}
			catch (Exception ex)
			{
				ex.WriteErrorLog(new string[0]);
				this.WriteErrMsg("", "页面出错", "/Try/ShowMytestProduct.aspx");
			}
		}
		if (!base.IsPostBack)
		{
			this.BindProvince();
		}
		this.BindCity(this.DropProvince.SelectedValue);
		this.BindAddress();
		this.ShowUserInfo();
		if (this.AddressList.Items.Count <= 0)
		{
			this.tabAddress.Visible = true;
			this.IsNewAddress = true;
		}
		else
		{
			this.SelectAddress.Visible = true;
			this.IsNewAddress = false;
			this.SelOldAddress.Visible = true;
		}
	}
	public void BindProvince()
	{
		DataTable dataTable = this.conn.GetDataTable("select Province from HLShop_CityArea group by Province");
		this.DropProvince.DataSource = dataTable;
		this.DropProvince.DataTextField = "Province";
		this.DropProvince.DataValueField = "Province";
		this.DropProvince.DataBind();
		this.DropProvince.SelectedIndex = 0;
		dataTable.Dispose();
	}
	public void BindAddress()
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_Address where UserID=" + base.user.UserID);
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			this.AddressList.DataSource = dataTable;
			this.AddressList.DataTextField = "Address";
			this.AddressList.DataValueField = "AddressID";
			this.AddressList.DataBind();
			this.AddressList.Items[0].Selected = true;
		}
		dataTable.Dispose();
	}
	public void BindCity(string Value)
	{
		DataTable dataTable = this.conn.GetDataTable("select City from HLShop_CityArea where Province='" + Value + "'");
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			this.DropCity.DataSource = dataTable;
			this.DropCity.DataTextField = "City";
			this.DropCity.DataValueField = "City";
			this.DropCity.DataBind();
		}
		dataTable.Dispose();
	}
	public void ShowUserInfo()
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HL_User where UserID=" + base.user.UserID);
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			this.UserName = dataTable.Rows[0]["UserName"].ToString();
			if (int.Parse(dataTable.Rows[0]["sex"].ToString()) == 0)
			{
				this.RadioSex.Items[0].Selected = true;
			}
			else
			{
				if (int.Parse(dataTable.Rows[0]["sex"].ToString()) == 1)
				{
					this.RadioSex.Items[1].Selected = true;
				}
				else
				{
					this.RadioSex.Items[0].Selected = true;
				}
			}
		}
		dataTable.Dispose();
	}
	protected void NewAddress_Click(object sender, EventArgs e)
	{
		this.IsNewAddress = true;
		this.tabAddress.Visible = true;
		this.SelectAddress.Visible = false;
	}
	protected void SelOldAddress_Click(object sender, EventArgs e)
	{
		this.IsNewAddress = false;
		this.tabAddress.Visible = false;
		this.SelectAddress.Visible = true;
	}
	protected void Submit_Click(object sender, ImageClickEventArgs e)
	{
		int num = 0;
		int num2 = 0;
		if (this.ProductID != 0)
		{
			if (this.conn.ExecuteCount(string.Concat(new object[]
			{
				"select * from HLShop_TryProduct where TryID=",
				this.ProductID,
				" and SalerID=",
				base.user.UserID
			})) > 0)
			{
				base.ClientScript.RegisterClientScriptBlock(typeof(Page), "", "<script>alert('自己的商品不能申请！')</script>");
			}
			else
			{
				if (this.IsApplication(base.user.UserID, this.ProductID))
				{
					base.ClientScript.RegisterClientScriptBlock(typeof(Page), "", "<script>alert('你已经申请过该产品，无需再申请！')</script>");
				}
				else
				{
					if (this.ApplicationBox.Text == "")
					{
						num++;
						this.lbAppliError.Text = "申请理由不能为空！";
					}
					else
					{
						if (this.ApplicationBox.Text.Length > 500)
						{
							num++;
							this.lbAppliError.Text = "申请理由不能超过500字！";
						}
					}
					if (this.IsNewAddress)
					{
						if (this.ZipCodeBox.Text == "")
						{
							num++;
							this.lbCodeError.Text = "邮政编码不能为空！";
						}
						else
						{
							if (!this.IsPostCode(this.ZipCodeBox.Text))
							{
								num++;
								this.lbCodeError.Text = "邮政编码的格式不对！";
							}
							else
							{
								this.lbCodeError.Text = "";
							}
						}
						if (this.AddressBox.Text == "")
						{
							num++;
							this.lbAddressError.Text = "收货地址不能为空！";
						}
						else
						{
							if (this.AddressBox.Text.Length > 100)
							{
								num++;
								this.lbAddressError.Text = "收货地址不能超过100字！";
							}
							else
							{
								this.lbAddressError.Text = "";
							}
						}
						if (this.UserNameBox.Text == "")
						{
							num++;
							this.lbNameError.Text = "收货名称不能为空！";
						}
						else
						{
							if (this.UserNameBox.Text.Length > 10)
							{
								num++;
								this.lbNameError.Text = "收货名称不能多与10个字！";
							}
							else
							{
								this.lbNameError.Text = "";
							}
						}
						if (this.TelBox.Text == "" && this.HomeTelBox.Text == "")
						{
							num++;
							this.lbTelError.Text = "联系方式最少需填写一种！";
						}
						else
						{
							if (!this.IsMobile(this.TelBox.Text) && this.TelBox.Text != "")
							{
								num++;
								this.lbTelError.Text = "手机号码格式不对！";
							}
							else
							{
								if (!this.IsHomePhone(this.HomeTelBox.Text) && this.HomeTelBox.Text != "")
								{
									num++;
									this.lbTelError.Text = "电话号码格式不对！";
								}
								else
								{
									this.lbTelError.Text = "";
								}
							}
						}
					}
					else
					{
						try
						{
							num2 = int.Parse(this.AddressList.SelectedValue);
						}
						catch (Exception ex)
						{
							ex.WriteErrorLog(new string[]
							{
								"正常操作"
							});
							num2 = 0;
						}
					}
					if (num > 0)
					{
						this.lbTotalError.Text = "共" + num + "处错误！";
					}
					else
					{
						this.lbTotalError.Text = "";
						if (this.AgreeMent.Checked)
						{
							if (this.IsNewAddress)
							{
								string text = this.HomeTelBox.Text;
								try
								{
									this.conn.Execute(string.Concat(new object[]
									{
										"insert into HLShop_TryAppcation ([UserID],[UserName],[ProductID],[AddressID],[CreateTime],[ApplicationReason],[Through],[DeliverStatus]) values(",
										base.user.UserID,
										",'",
										base.user.UserName,
										"',",
										this.ProductID,
										",",
										num2,
										",'",
										DateTime.Now,
										"','",
										this.ApplicationBox.Text,
										"',0,0)"
									}));
									this.conn.Execute(string.Concat(new object[]
									{
										"insert into HLShop_Address([UserID],[Province],[City],[ZipCode],[Address],[UserName],[MobilePhone],[ConectPhone]) values(",
										base.user.UserID,
										",'",
										this.DropProvince.SelectedValue,
										"','",
										this.DropCity.SelectedValue,
										"','",
										this.ZipCodeBox.Text,
										"','",
										this.AddressBox.Text,
										"','",
										this.UserNameBox.Text,
										"','",
										this.TelBox.Text,
										"','",
										text,
										"')"
									}));
									if (num2 == 0 && this.IsNewAddress)
									{
										DataTable dataTable = this.conn.GetDataTable("select max(AddressID) as AddID from HLShop_Address");
										this.conn.Execute("Update HLShop_TryAppcation set AddressID=" + int.Parse(dataTable.Rows[0]["AddID"].ToString()));
									}
								}
								catch (Exception ex)
								{
									ex.WriteErrorLog(new string[]
									{
										"正常操作"
									});
									this.WriteErrMsg("", "申请失败,请重新填写", "TryApplication.aspx");
								}
							}
							else
							{
								try
								{
									this.conn.Execute(string.Concat(new object[]
									{
										"insert into HLShop_TryAppcation ([UserID],[UserName],[ProductID],[AddressID],[CreateTime],[ApplicationReason],[Through],[DeliverStatus]) values(",
										base.user.UserID,
										",'",
										base.user.UserName,
										"',",
										this.ProductID,
										",",
										num2,
										",'",
										DateTime.Now,
										"','",
										this.ApplicationBox.Text,
										"',0,0)"
									}));
								}
								catch (Exception ex)
								{
									ex.WriteErrorLog(new string[]
									{
										"正常操作"
									});
									this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('申请失败,请重新申请!');</script>");
									return;
								}
							}
							this.WriteSuccessMsg("", "恭喜你，申请成功！", "../Member/Shop_MytryItem.aspx?tag=all");
						}
						else
						{
							this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('请先阅读协议!');</script>");
						}
					}
				}
			}
		}
		else
		{
			base.ClientScript.RegisterClientScriptBlock(typeof(Page), "", "<script>alert('该商品暂时无法申请！')</script>");
		}
	}
	public bool IsMobile(string StrTel)
	{
		return Regex.IsMatch(StrTel, "^1[3458]\\d{9}$", RegexOptions.IgnoreCase);
	}
	public bool IsPostCode(string ZipCode)
	{
		return Regex.IsMatch(ZipCode, "^\\d{6}$", RegexOptions.IgnoreCase);
	}
	public bool IsHomePhone(string HomePhone)
	{
		return Regex.IsMatch(HomePhone, "^\\d{3,4}-?\\d{6,8}$", RegexOptions.IgnoreCase);
	}
	public bool IsApplication(int UserID, int ProductID)
	{
		return this.conn.ExecuteCount(string.Concat(new object[]
		{
			"select count(*) from HLShop_TryAppcation where UserID=",
			UserID,
			" and ProductID=",
			ProductID
		})) > 0;
	}
}
